import axios from 'axios'
import { ElMessage } from 'element-plus'

const request = axios.create({
  baseURL: "http://140.143.150.79:8080/api",
  timeout: 5000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 响应拦截器
request.interceptors.response.use(
  response => {
    console.log(response)
    const { code, message, data } = response.data
    
    if (code === 200) {
      return data
    }
    
    // 统一处理非200状态码
    ElMessage.error(message || '请求失败')
    return Promise.reject(new Error(message || '请求失败'))
  },
  error => {
    ElMessage.error(error.message || '网络错误')
    return Promise.reject(error)
  }
)

export default request 